import 'package:crydit_ume/flutter_ume.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_demo/core/theme/theme.dart';
import 'package:flutter_app_demo/core/widgets/counter_provider.dart';
import 'package:flutter_app_demo/pages/bloc_demo/view.dart';
import 'package:flutter_app_demo/pages/flame_example/flame_demo_list/view.dart';
import 'package:flutter_app_demo/pages/login/login_page.dart';
import 'package:flutter_app_demo/pages/performance_Issue/performance_Issue_page.dart';
import 'package:flutter_app_demo/pages/plugin_page/world_cup_page.dart';
import 'package:flutter_app_demo/pages/widgets_page/wigets_page.dart';
import 'package:flutter_app_demo/service/provider_list.dart';
import 'package:flutter_app_demo/service/shared_prefs/app_prefs.dart';
import 'package:flutter_ume_kit_console/flutter_ume_kit_console.dart';
import 'package:flutter_ume_kit_perf/flutter_ume_kit_perf.dart';
import 'package:provider/provider.dart';
import 'package:flutter_ume_kit_dio/flutter_ume_kit_dio.dart';
import 'core/manager/app_theme_manager.dart';
import 'core/route/router.dart';
import 'core/utils/dio_util.dart';
import 'pages/home/home.dart';

void main() {
  final appProviderList = createAppProviderList();
  runApp(ManagerWrapper(
      child: MultiProvider(providers: appProviderList, child: MyApp())));
}

class ManagerWrapper extends StatelessWidget {
  final Widget child;

  const ManagerWrapper({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return AppThemeScope(
      notifier: AppThemeManager(),
      child: child,
    );
  }
}

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final dio = DioUtil().dio;

  @override
  void initState() {
    super.initState();
    PluginManager.instance
      ..register(DioInspector(dio: dio))
      ..register(Console())
      ..register(MemoryInfoPage())
      ..register(Performance());
  }

  @override
  Widget build(BuildContext context) {
    AppThemeManager appThemeManager = AppThemeScope.of(context);
    return UMEWidget(
      enable: true,
      child: MaterialApp.router(
        debugShowCheckedModeBanner: false,
        title: 'App Demo',
        themeMode: appThemeManager.mode,
        darkTheme: AppTheme.getDarkThemeData(),
        theme: AppTheme.getLightThemeData(),
        routerConfig: AppRouter.returnRouter(true),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late AppPrefs appPrefs;
  int _selectedIndex = 0;
  int count = 0;
  bool _isLoading = true; // 添加加载状态

  final List<String> _titles = [
    'Home',
    '不常用组件',
    '登录界面',
    '模拟卡顿',
    '插件开发',
    'Flame',
    'Flutter Bloc',
  ];

  @override
  void initState() {
    super.initState();
    loadMenuIndex();
  }

  Future loadMenuIndex() async {
    appPrefs = AppPrefs();
    _selectedIndex = await appPrefs.loadHomePageMenuIndex();
    setState(() {
      _isLoading = false; // 加载完成后更新状态
    });
  }

  final List<Widget> _pages = [
    // ChangeNotifierProvider(
    //     create: (context) => CounterService(),
    //     child: ),
    const MyHomePage(title: 'My Home Page'),
    const WidgetsPage(),
    const LoginPage(),
    const PerformanceIssuePage(),
    // const PerformanceIssueComputePage(),
    const WorldCupPage(),
    const FlameDemoListPage(),
    BlocDemoPage(),
  ];

  @override
  Widget build(BuildContext context) {
    if (_isLoading) {
      // 加载过程中显示加载指示器
      return Scaffold(
        appBar: AppBar(
          title: const Text('Loading...'),
        ),
        body: const Center(
          child: CircularProgressIndicator(),
        ),
      );
    }

    return CountProvider(
      count: count,
      onIncremwnt: () => setState(() => count++),
      child: Scaffold(
        appBar: AppBar(
          title: Text(_titles[_selectedIndex]),
        ),
        drawer: Drawer(
          child: ListView(
            padding: EdgeInsets.zero,
            children: <Widget>[
              const DrawerHeader(
                decoration: BoxDecoration(
                  color: Colors.blue,
                ),
                child: Text('Menu'),
              ),
              for (int i = 0; i < _titles.length; i++)
                ListTile(
                  title: Text(_titles[i]),
                  selected: _selectedIndex == i,
                  onTap: () {
                    setState(() {
                      _selectedIndex = i;
                      appPrefs.saveHomePageMenuIndex(_selectedIndex);
                    });
                    Navigator.pop(context); // 关闭抽屉
                  },
                ),
            ],
          ),
        ),
        body: _pages[_selectedIndex],
        // bottomNavigationBar: BottomNavigationBar(
        //   currentIndex: _selectedIndex,
        //   onTap: (index) {
        //     setState(() {
        //       _selectedIndex = index;
        //       appPrefs.saveHomePageMenuIndex(_selectedIndex);
        //     });
        //   },
        //   items: const [
        //     BottomNavigationBarItem(
        //       backgroundColor: Colors.black,
        //       icon: Icon(Icons.home),
        //       label: 'Home',
        //     ),
        //     BottomNavigationBarItem(
        //       backgroundColor: Colors.black,
        //       icon: Icon(Icons.widgets),
        //       label: '不常用组件',
        //     ),
        //     BottomNavigationBarItem(
        //       backgroundColor: Colors.black,
        //       icon: Icon(Icons.login),
        //       label: '登录界面',
        //     ),
        //     BottomNavigationBarItem(
        //       backgroundColor: Colors.black,
        //       icon: Icon(Icons.person),
        //       label: '模拟卡顿',
        //     ),
        //   ],
        // ),
      ),
    );
  }
}
